package com.campusbackend.service.impl;

import com.campusbackend.entity.NewsManagement;
import com.campusbackend.mapper.NewsManagementMapper;
import com.campusbackend.service.NewsManagementService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 新闻管理表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-29
 */
@Slf4j
@Service
public class NewsManagementServiceImpl extends ServiceImpl<NewsManagementMapper, NewsManagement> implements NewsManagementService {

    @Autowired
    private NewsManagementMapper newsManagementMapper;

    @Override
    public IPage<NewsManagement> getNewsPage(int page, int size, Integer status) {
        Page<NewsManagement> pageObj = new Page<>(page, size);
        return newsManagementMapper.selectPageWithStatus(pageObj, status);
    }

    @Override
    public NewsManagement getNewsById(Long id) {
        return newsManagementMapper.selectByIdNotDeleted(id);
    }

    @Override
    public NewsManagement createNews(String title, String content, String summary, 
                                   Integer status, Long publisherId) {
        NewsManagement news = new NewsManagement();
        news.setTitle(title);
        news.setContent(content);
        news.setSummary(summary);
        news.setStatus(status);
        news.setPublisherId(publisherId);
        news.setViewCount(0);
        
        LocalDateTime now = LocalDateTime.now();
        news.setCreateTime(now);
        news.setUpdateTime(now);
        
        // 如果状态是已发布，设置发布时间
        if (status != null && status == 1) {
            news.setPublishTime(now);
        }
        
        boolean saved = save(news);
        if (!saved) {
            throw new RuntimeException("新闻创建失败");
        }
        
        return news;
    }

    @Override
    public NewsManagement updateNews(Long id, String title, String content, String summary, Integer status) {
        NewsManagement news = getNewsById(id);
        if (news == null) {
            throw new IllegalArgumentException("新闻不存在");
        }
        
        // 更新字段
        if (title != null && !title.trim().isEmpty()) {
            news.setTitle(title);
        }
        if (content != null) {
            news.setContent(content);
        }
        if (summary != null) {
            news.setSummary(summary);
        }
        if (status != null) {
            // 如果从草稿状态改为发布状态，设置发布时间
            if (news.getStatus() == 0 && status == 1) {
                news.setPublishTime(LocalDateTime.now());
            }
            news.setStatus(status);
        }
        
        news.setUpdateTime(LocalDateTime.now());
        
        boolean updated = updateById(news);
        if (!updated) {
            throw new RuntimeException("新闻更新失败");
        }
        
        return news;
    }

    @Override
    public boolean deleteNews(Long id) {
        NewsManagement news = getNewsById(id);
        if (news == null) {
            throw new IllegalArgumentException("新闻不存在");
        }
        
        return newsManagementMapper.softDeleteById(id) > 0;
    }

    @Override
    public void incrementViewCount(Long id) {
        newsManagementMapper.incrementViewCount(id);
    }
}
